package com.batch.demo.domain.ParametersDemo;

import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class ParametersDemo implements StepExecutionListener {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    private Map<String, JobParameter> parameters;

    @Bean
    public Job parametersJob(){
        return jobBuilderFactory.get("parametersJob")
                .start(parametersStep())
                .build();

    }


    // 给step传递数据可以用监听的方式
    //
    @Bean
    public Step parametersStep(){
        return stepBuilderFactory.get("parametersStep")
                .listener(this)
                .tasklet(((stepContribution, chunkContext) -> {
                    System.out.println("paraters : " + parameters.get("info"));// 查找map里某健的值
                    return RepeatStatus.FINISHED;
                })).build();
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        parameters = stepExecution.getJobParameters().getParameters();
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }
}
